package com.zeng.jiudian.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.bean.UserModel;
import com.zeng.jiudian.service.UserService;
import com.zeng.jiudian.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/21 - 16:44
 */
@WebServlet("/back/user/findAll")
public class UserFindAllServlet extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        String username = req.getParameter("username");
        String startBirthday = req.getParameter("startBirthday");
        String endBirthday = req.getParameter("endBirthday");
        String introduce = req.getParameter("introduce");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String order = req.getParameter("order");
        String lift = req.getParameter("lift");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        String nowNum = req.getParameter("nowNum");
        PageModel pageModel = new PageModel();
        pageModel.setPageNum((pageNum == null || "".equals(pageNum) || "0".equals(pageNum)) ? 1 : Integer.parseInt(pageNum));
        pageModel.setPageSize((pageSize == null || "".equals(pageSize) || "0".equals(pageSize)) ? 10 : Integer.parseInt(pageSize));
        pageModel.setNowNum((nowNum == null || "".equals(nowNum) || "0".equals(nowNum)) ? 1 : Integer.parseInt(nowNum));

        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setStartBirthday(startBirthday);
        userModel.setEndBirthday(endBirthday);
        userModel.setIntroduce(introduce);
        userModel.setSex(sex);
        userModel.setPhone(phone);
        userModel.setOrder(order);
        userModel.setLift(lift);
        userModel.setPageModel(pageModel);

        UserService userService = new UserServiceImpl();
        List<UserModel> list = userService.findAll(userModel);
        int count = userService.getCount(userModel);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
